package com.touzhijia.authority.controller;

import com.touzhijia.authority.domain.dto.request.AuthenticationReq;
import com.touzhijia.authority.domain.dto.request.AuthorizationReq;
import com.touzhijia.authority.domain.dto.response.AuthenticationRes;
import com.touzhijia.authority.domain.dto.response.AuthorizationRes;
import com.touzhijia.authority.domain.entity.Permission;
import com.touzhijia.authority.domain.entity.Role;
import com.touzhijia.authority.service.AuthorityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者： lzw<br/>
 * 创建时间：2018-03-20 16:04 <br/>
 */
@Api(description = "权限控制")
@RestController
@RequestMapping("/api")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    @ApiOperation("查询用户所有的角色")
    @GetMapping("/auth/{username}/role")
    public List<Role> findUserAllRole(@PathVariable("username") String username) {
        return authorityService.findUserAllRole(username);
    }

    @ApiOperation("查询用户所有的角色")
    @GetMapping("/auth/{username}/role_str")
    public List<String> findUserAllRoleStr(@PathVariable("username") String username) {
        List<String> result = new ArrayList<>();
        List<Role> list = authorityService.findUserAllRole(username);
        for (Role role : list) {
            result.add(role.getName());
        }
        return result;
    }

    @ApiOperation("查询用户所有的权限")
    @GetMapping("/auth/{username}/permission")
    public List<Permission> findUserAllPermission(@PathVariable("username") String username) {
        return authorityService.findUserAllPermission(username);
    }

    @ApiOperation("查询用户所有的权限")
    @GetMapping("/auth/{username}/permission_str")
    public List<String> findUserAllPermissionStr(@PathVariable("username") String username) {
        List<String> result = new ArrayList<>();
        List<Permission> list = authorityService.findUserAllPermission(username);
        for (Permission permission : list) {
            result.add(permission.getPermission());
        }
        return result;
    }

    @ApiOperation("查询用户在指定模块的权限")
    @GetMapping("/auth/{username}/permission/{module}")
    public List<Permission> findUserAllPermission(@PathVariable("username") String username, @PathVariable("module") String module) {
        return authorityService.findUserAllPermission(username, module);
    }

    @ApiOperation("查询用户在指定模块的权限")
    @GetMapping("/auth/{username}/permission_str/{module}")
    public List<String> findUserAllPermissionStr(@PathVariable("username") String username, @PathVariable("module") String module) {
        List<String> result = new ArrayList<>();
        List<Permission> list = authorityService.findUserAllPermission(username, module);
        for (Permission permission : list) {
            result.add(permission.getPermission());
        }
        return result;
    }

    @ApiOperation("用户身份认证")
    @PostMapping("/auth/authentication")
    public AuthenticationRes authentication(@Validated @RequestBody AuthenticationReq authenticationReq) {
        return authorityService.authentication(null, authenticationReq);
    }

    @ApiOperation("用户身份认证(使用模块名称)")
    @PostMapping("/auth/authentication/{module}")
    public AuthenticationRes authentication(@PathVariable("module") String module, @Validated @RequestBody AuthenticationReq authenticationReq) {
        return authorityService.authentication(module, authenticationReq);
    }

    @ApiOperation("用户权限认证")
    @PostMapping("/auth/authorization")
    public AuthorizationRes authorization(@Validated @RequestBody AuthorizationReq authorizationReq) {
        return authorityService.authorization(authorizationReq);
    }

    @ApiOperation("查询全部权限")
    @GetMapping("/auth/permission")
    public List<Permission> findAllPermission() {
        return authorityService.findAllPermission();
    }

    @ApiOperation("查询一个模块的所有权限")
    @GetMapping("/auth/permission/{module}")
    public List<Permission> findAllPermission(@PathVariable("module") String module) {
        return authorityService.findAllPermission(module);
    }
}
